// 问题管理
// 导入数据库
const db = require("../db/index");

/**
 * 发表提问
 * @param {title message} req
 * @param {status 0 } res
 */
exports.addReq = (req, res) => {
  const reqData = {
    ...req.body,
    moment: new Date(),
    // 作者id
    user_id: req.user.id,
  };
  const sql = ` insert into request set ?`;
  db.query(sql, reqData, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("发布提问失败");
    res.cc("发布提问成功", 0);
  });
};

/**
 * 获取所有未被删除的问题
 * way 1 模糊查询 0 正常  2按时间 1模糊查 0按评论数
 *test 内容  query
 * @param {way text} req
 * @param {*} res
 */
exports.getReq = (req, res) => {
  const sql = `select request.* ,users.username from request,users where  request.is_delete != 1 and request.user_id = users.id order by request.comment_num desc`;
  const sql2 = `select request.* ,users.username from request,users where request.is_delete != 1 and request.user_id = users.id and  request.title like ? order by request.moment desc`;
  const sql3 = `select request.* ,users.username from request,users where request.is_delete != 1 and request.user_id = users.id and  request.title like ? order by request.moment desc`;
  let mysql = req.query.way == 1 ? sql2 : sql;

  const mydata = req.query.text;
  //  ---------为什么这里换个位置就不行了
  if (req.query.way == 2) {
    mysql = sql3;
  }

  db.query(mysql, [`%${mydata}%`], (err, results) => {
    if (err) return res.cc(err);
    res.send({
      message: "获取提问成功",
      status: 0,
      data: results,
    });
  });
};

/**
 * 获取用户发布的个人问题
 * @param {user_id} req
 * @param {id user_id username title message moment comment__num view_num is_delete } res
 */
exports.getMyReq = (req, res) => {
  const sql = `select * from request where user_id = ? and is_delete !=1  order by moment desc`;
  db.query(sql, req.user.id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      message: "获取成功",
      status: 0,
      data: results,
    });
  });
};

/**
 * 删除用户提问卡片
 *
 * @param {req_id} req
 * @param {*} res
 */
exports.deleteMyReq = (req, res) => {
  const sql = `update request set is_delete = ? where id = ? `;
  db.query(sql, [1, req.query.req_id], (err, results) => {
    if (err) return err;
    if (results.affectedRows !== 1) return res.cc("删除失败！稍后再试");
    res.send({
      status: 0,
      message: "删除成功！",
    });
  });
};

/**
 *  用户点击增加浏览量
 * params
 * @param {request_id} req
 * @param {*} res
 */
exports.updateReqView = (req, res) => {
  const sql = `update request set view_num = view_num + 1 where id = ? `;
  db.query(sql, req.query.request_id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("浏览错误");
    res.cc("更新浏览成功", 0);
  });
};

/**
 * 获取用户浏览过的问题
 * @param {*} req
 * @param {*} res
 */
exports.findMyViewReq = (req, res) => {
  const sql = `select * from request where id in (select req_id where user_id = ?)`;
  db.query(sql, req.user.id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      message: "获取成功",
      status: 0,
      data: results,
    });
  });
};

